package com.mxm.mirai.quartz;

import com.mxm.mirai.config.MXM_config;
import com.mxm.mirai.service.ACGImgsService;
import com.mxm.mirai.service.BotconfigService;
import com.mxm.mirai.service.BotgroupsService;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.ServiceMessage;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;
//@Component
public class Quartz_AcgRandomImg_Run extends QuartzJobBean {
    @Autowired
    BotgroupsService botgroupsService;
    @Autowired
    BotconfigService botconfigService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
                ACGImgsService acgimg = new ACGImgsService();
                List<String> imgs = acgimg.getWallhavenImgUrl(acgimg.getWallhavenImgSUrl());

//                List<String> imgs = acgimg.getPicturesImgUrl(acgimg.getPicturesImgSUrl());
                if(imgs!=null && imgs.size()!=0){
                    for (Bot bot : Bot.getBotInstances()) {
                        for (Group group:bot.getGroups()){
                            if("1".equals(botgroupsService.QueryBotGroupsState(String.valueOf(bot.getId()),String.valueOf(group.getId()),"AcgRandomImg"))) {
                                //=======================================================================
                                for (String imgurl : imgs) {
                                    MXM_config.log.BotLog(String.valueOf(bot.getId()),"获取到随机二次元图片:" + imgurl,true);
                                    if (bot.isOnline()) {//在线则发送
                                        Image img = group.uploadImage(acgimg.getURLFile(imgurl));
                                        String imgid = img.getImageId().replace("{", "").replace("}", "").replace(".mirai", "").replace("-", "");
                                        if("1".equals(botconfigService.QueryBotConfigState(String.valueOf(bot.getId()),"messageformat"))){
                                            group.sendMessage(new ServiceMessage(1, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><msg serviceID=\"5\" templateID=\"1\" action=\"\" brief=\"这只是一张图片\" sourceMsgId=\"0\" url=\"\" flag=\"2\" adverSign=\"0\" multiMsgFlag=\"0\"><item layout=\"0\" advertiser_id=\"0\" aid=\"0\"><image uuid=\"" + imgid + ".jpg\" md5=\"" + imgid + "\" GroupFiledid=\"536321009\" filesize=\"426304\" local_path=\"/storage/emulated/0/Android/data/com.tencent.mobileqq/Tencent/MobileQQ/chatpic/chatimg/b21/Cache_" + imgid + "\" minWidth=\"400\" minHeight=\"400\" maxWidth=\"500\" maxHeight=\"1000\" /></item><source name=\"\" icon=\"\" action=\"\" appid=\"-1\" /></msg>"));
                                        }else {
                                            group.sendMessage(img);
                                        }
                                        MXM_config.log.BotLog(String.valueOf(bot.getId()),"随机二次元图片发送成功。",true);
                                    }
                                }
                                //=======================================================================
                            }
                        }
                    }
                }
        }catch (Exception e){
        }
    }
}
